package com.ekuaitu.kuaitu.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ekuaitu.kuaitu.R;
import com.ekuaitu.kuaitu.api.ApiStore;
import com.ekuaitu.kuaitu.api.UrlConstant;
import com.ekuaitu.kuaitu.base.BaseActivity;
import com.ekuaitu.kuaitu.bean.PersonalCenterBean;
import com.ekuaitu.kuaitu.bean.ResultBean;
import com.ekuaitu.kuaitu.utils.AppManager;
import com.ekuaitu.kuaitu.utils.Constants;
import com.ekuaitu.kuaitu.utils.ImageLoader;
import com.ekuaitu.kuaitu.utils.IntentKey;
import com.ekuaitu.kuaitu.utils.MyApplication;
import com.ekuaitu.kuaitu.utils.SharedPreferencesKey;
import com.ekuaitu.kuaitu.utils.SharedPreferencesUtils;
import com.ekuaitu.kuaitu.utils.Toast.ToastCompat;
import com.ekuaitu.kuaitu.widget.EasyTransition;
import com.ekuaitu.kuaitu.widget.EasyTransitionOptions;
import com.ekuaitu.kuaitu.widget.MarqueeView;
import com.ekuaitu.kuaitu.widget.MyScrollView;
import com.ekuaitu.kuaitu.widget.NumberRunningTextView;
import com.umeng.analytics.MobclickAgent;
import com.zhy.autolayout.AutoRelativeLayout;

import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class PersonalCenterActivity extends BaseActivity {

    @BindView(R.id.backdrop)
    ImageView backdrop;
    @BindView(R.id.title_center)
    AutoRelativeLayout title_center;
    @BindView(R.id.scrollView_center)
    MyScrollView scrollview_center;
    @BindView(R.id.scrollView_center_unSign)
    MyScrollView scrollview_centerUnsign;
    @BindView(R.id.myInfo_head)
    CircleImageView myInfoHead;
    @BindView(R.id.center_username)
    TextView centerUsername;
    @BindView(R.id.center_speaker)
    ImageView centerSpeaker;
    @BindView(R.id.center_more)
    ImageView centerMore;
    @BindView(R.id.center_count)
    NumberRunningTextView centerCount;
    @BindView(R.id.center_coupon)
    NumberRunningTextView centerCoupon;
    @BindView(R.id.center_card)
    NumberRunningTextView centerCard;
    @BindView(R.id.center_violate)
    NumberRunningTextView centerViolate;
    @BindView(R.id.center_certify_status)
    ImageView center_certify_status;
    @BindView(R.id.myInfo_member)
    ImageView myInfo_member;
    @BindView(R.id.center_back)
    ImageView returnAccount;
    private Context mContext = this;
    @BindView(R.id.progress_bar_myInfo)
    RelativeLayout progressBarMyInfo;
    @BindView(R.id.scroll_textView)
    MarqueeView scroll_textView;
    @BindView(R.id.center_login)
    TextView center_login;
    @BindView(R.id.center_unit4)
    TextView centerUnit4;
    @BindView(R.id.mes_red_hot)
    ImageView mesRedHot;
    @BindView(R.id.center_member)
    ImageView centerMember;
    private int memberType;

    @Override
    public int getLayoutId() {
        return R.layout.activity_personal_center;
    }

    @Override
    public void initData() {

    }

    private void getData() {
        progressBarMyInfo.setVisibility(View.VISIBLE);
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).getMyInfoData(MyApplication.getInstance().getRequestFieldMap()).enqueue(new Callback<PersonalCenterBean>() {
            @Override
            public void onResponse(Call<PersonalCenterBean> call, final Response<PersonalCenterBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    progressBarMyInfo.setVisibility(View.GONE);
                    if (response.body().getStatus() == 200) {
                        SharedPreferencesUtils.getInstance(mContext).putStringValue(SharedPreferencesKey.IDENTITY_STATUS, response.body().getAttachment().getIdentityStatus() + "");
                        SharedPreferencesUtils.getInstance(mContext).putStringValue(SharedPreferencesKey.LICENSE_STATUS, response.body().getAttachment().getLicenseStatus() + "");
                        DecimalFormat df = new DecimalFormat("0.00");
                        centerCount.setContent(df.format(response.body().getAttachment().getBalance() / 100));
                        centerCoupon.setContent(response.body().getAttachment().getCouponCount() + "");
                        centerCard.setContent(response.body().getAttachment().getCardCouponCount() + "");
                        centerUsername.setText(response.body().getAttachment().getUserName());
                        //违规次数
                        if (response.body().getAttachment().getViolationNum() == 0) {
                            centerUnit4.setVisibility(View.GONE);
                            centerViolate.setVisibility(View.GONE);
                        } else {
                            centerViolate.setContent(response.body().getAttachment().getViolationNum() + "");
                        }
                        //实名认证状态
                        if (response.body().getAttachment().getIdentityStatus() == 13) {
                            center_certify_status.setImageDrawable(getResources().getDrawable(R.drawable.certify_ok));
                        } else {
                            center_certify_status.setImageDrawable(getResources().getDrawable(R.drawable.certify_no));
                        }
                        //小红点
                        if ("1".equals(response.body().getAttachment().getIsRead())) {
                            mesRedHot.setVisibility(View.VISIBLE);
                        } else {
                            mesRedHot.setVisibility(View.GONE);
                        }

                        //滚动广告
                        if (response.body().getAttachment().getAdList() != null && response.body().getAttachment().getAdList().size() != 0) {
                            List strings = new ArrayList();
                            for (int i = 0; i < response.body().getAttachment().getAdList().size(); i++) {
                                strings.add(response.body().getAttachment().getAdList().get(i).getContent());
                            }
                            scroll_textView.startWithList(strings, null);
                            scroll_textView.setOnItemClickListener(new MarqueeView.OnItemClickListener() {
                                @Override
                                public void onItemClick(int position, LinearLayout textView) {
                                    MobclickAgent.onEvent(PersonalCenterActivity.this, Constants.USERCENTER_PUBLIC);
                                    Intent intent = new Intent(PersonalCenterActivity.this, WebViewActivity.class);
                                    intent.putExtra(IntentKey.INTENT_WEBVIEW_TITLE, response.body().getAttachment().getAdList().get(position).getH5Title());
                                    intent.putExtra(IntentKey.INTENT_WEBVIEW_URL, response.body().getAttachment().getAdList().get(position).getH5Url());
                                    startActivity(intent);
                                }
                            });
                        }
                        //季卡月卡活动图片
                        if (response.body().getAttachment().getIsShowConcessionCard() == 1) {
                            myInfo_member.setVisibility(View.VISIBLE);
                            ImageLoader.getInstance().displayImage(getApplicationContext(),
                                    MyApplication.getInstance().getImageUrl() + response.body().getAttachment().getConcessionCardPicture(), myInfo_member);
                        } else {
                            myInfo_member.setVisibility(View.GONE);
                        }

                        //季卡月卡会员
                        memberType = response.body().getAttachment().getUserVipType();
                        if (memberType == 0) {
                            centerMember.setVisibility(View.GONE);
                        } else if (memberType == 1) {
                            centerMember.setVisibility(View.VISIBLE);
                            centerMember.setImageDrawable(getResources().getDrawable(R.drawable.member_month));
                        } else if (memberType == 2) {
                            centerMember.setVisibility(View.VISIBLE);
                            centerMember.setImageDrawable(getResources().getDrawable(R.drawable.member_quarter));
                        }


                    } else {
                        ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void onFailure(Call<PersonalCenterBean> call, Throwable t) {
                progressBarMyInfo.setVisibility(View.GONE);
                ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void initView() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            this.getWindow().setStatusBarColor(Color.TRANSPARENT);
            this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        scrollview_center.setScrollViewListener(new MyScrollView.ScrollViewListener() {
            @Override
            public void onScrollChanged(MyScrollView scrollView, int x, int y, int oldx, int oldy) {
                if (y != 50) {
                    title_center.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
                }
                if (y == 0) {
                    title_center.setBackgroundColor(getResources().getColor(R.color.transparent));
                }
            }
        });
        scrollview_centerUnsign.setScrollViewListener(new MyScrollView.ScrollViewListener() {
            @Override
            public void onScrollChanged(MyScrollView scrollView, int x, int y, int oldx, int oldy) {
                if (y != 50) {
                    title_center.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
                }
                if (y == 0) {
                    title_center.setBackgroundColor(getResources().getColor(R.color.transparent));
                }
            }
        });
        myInfoHead.setImageDrawable(getResources().getDrawable(R.drawable.user_profile));

    }

    //根据登录的状显示相应的界面
    private void switchLoginStatus() {
        if (MyApplication.getInstance().getUid() == null) {
            scrollview_center.setVisibility(View.GONE);
            scrollview_centerUnsign.setVisibility(View.VISIBLE);
        } else {
            scrollview_center.setVisibility(View.VISIBLE);
            scrollview_centerUnsign.setVisibility(View.GONE);
            if (SharedPreferencesUtils.getInstance(mContext).getStringValue("avatarPath") != null) {
                String picPath = SharedPreferencesUtils.getInstance(mContext).getStringValue("avatarPath");
                File picFile = new File(picPath);
                ImageLoader.getInstance().displayCricleImage(getApplicationContext(), picFile, myInfoHead);
            } else if (SharedPreferencesUtils.getInstance(mContext).getStringValue("picture") != null
                    && !SharedPreferencesUtils.getInstance(mContext).getStringValue("picture").equals("0")) {
                ImageLoader.getInstance().displayCricleImage(getApplicationContext(), MyApplication.getInstance().getImageUrl() + SharedPreferencesUtils.getInstance(mContext).getStringValue("picture"), myInfoHead);
            } else {
                myInfoHead.setImageDrawable(getResources().getDrawable(R.drawable.user_profile));
            }
            getData();
        }
    }

    @OnClick({R.id.center_head, R.id.center_more, R.id.center_back, R.id.center_login, R.id.myInfo_wallet, R.id.myInfo_coupon, R.id.myInfo_card,
            R.id.myInfo_route, R.id.myInfo_message, R.id.myInfo_violate, R.id.myInfo_staff, R.id.myInfo_invite, R.id.myInfo_member})
    public void onClick(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.center_head:
            case R.id.center_more:
                EasyTransitionOptions options = EasyTransitionOptions.
                        makeTransitionOptions(this, findViewById(R.id.center_head));
                intent.setClass(mContext, PersonalInfoActivity.class);
                EasyTransition.startActivity(intent, options);
                break;
            case R.id.center_back:
                AppManager.getAppManager().finishActivity(this);
                break;
            case R.id.center_login:
                intent.setClass(mContext, LoginActivity.class);
                startActivity(intent);
                break;
            case R.id.myInfo_wallet:
                MobclickAgent.onEvent(this, Constants.PURSE);
                intent.setClass(mContext, MyWalletActivity.class);
                startActivity(intent);
                break;
            case R.id.myInfo_coupon:
                intent.setClass(mContext, MyCouponActivity.class);
                startActivity(intent);
                break;
            case R.id.myInfo_card:
                intent.setClass(mContext, MyCardActivity.class);
                startActivity(intent);
                break;
            case R.id.myInfo_route:
                MobclickAgent.onEvent(this, Constants.TRAVEL);
                intent.setClass(mContext, MyRouteActivity.class);
                startActivity(intent);
                break;
            case R.id.myInfo_message:
                readAllMsg();
                mesRedHot.setVisibility(View.GONE);
                intent.setClass(mContext, MyMessageActivity.class);
                startActivity(intent);
                break;
            case R.id.myInfo_violate:
                intent.setClass(mContext, ViolationRecordedActivity.class);
                startActivity(intent);
                break;
            case R.id.myInfo_staff:
                intent.setClass(mContext, MyStaffActivity.class);
                startActivity(intent);
                MobclickAgent.onEvent(this, Constants.CUSTOMER_SERVICE);
                break;
            case R.id.myInfo_invite:
                intent.setClass(mContext, InviteActivity.class);
                intent.putExtra(IntentKey.SHARE_TYPE, 2);
                startActivity(intent);
                break;
            case R.id.myInfo_member:
                intent.setClass(mContext, MemberActivity.class);
                intent.putExtra("memberType", memberType);
                startActivity(intent);
                break;
        }
    }

    private void readAllMsg() {
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).readAllMsg(MyApplication.getInstance().getRequestFieldMap()).enqueue(new Callback<ResultBean>() {
            @Override
            public void onResponse(Call<ResultBean> call, Response<ResultBean> response) {
            }

            @Override
            public void onFailure(Call<ResultBean> call, Throwable t) {
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        switchLoginStatus();
    }

}
